package hl.demosample.ui.fragment.photoview

import android.content.ContentValues
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.transition.Transition
import com.github.chrisbanes.photoview.OnOutsidePhotoTapListener
import com.github.chrisbanes.photoview.OnPhotoTapListener
import hl.demosample.R
import hl.demosample.app.App
import hl.demosample.app.base.BaseFragment
import hl.demosample.app.ext.initClose
import hl.demosample.app.util.DownloadPicture
import hl.demosample.app.weight.downloadpicture.*
import hl.demosample.databinding.FragmentPhotoViewBinding
import hl.demosample.viewmodel.state.PhotoViewModel
import kotlinx.android.synthetic.main.fragment_photo_view.*
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.ext.nav
import java.io.*

/*
* path: DemoSample 
* Author: hl
* Create: 2021-08-17 16:09
* Changes (from 2021-08-17)
* Desc:
*/
class PhotoViewFragment : BaseFragment<PhotoViewModel, FragmentPhotoViewBinding>() {

    private var mUrl: String? = ""

    override fun layoutId() = R.layout.fragment_photo_view

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.click = ProxyClick()
        toolbar.run {
            initClose("查看大图") {
                nav().navigateUp()
            }
            inflateMenu(R.menu.photoview_menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.item_download -> {
                        mUrl?.let {
                            Log.d("mUrl",""+it)
                            DownloadPicture.downloadPicture(it,App.instance)
                        }
                    }
                }
                true
            }
        }
        arguments?.let {
            mUrl = it.getString("url") ?: ""
        }
        Glide.with(this).load(mUrl).into(photoView)
    }

    inner class ProxyClick {
        fun photoView() {
            photoView.setOnPhotoTapListener(object : OnOutsidePhotoTapListener, OnPhotoTapListener {
                override fun onOutsidePhotoTap(p0: ImageView?) {
                }

                override fun onPhotoTap(p0: ImageView?, p1: Float, p2: Float) {
                    nav().navigateUp()
                }
            })
        }
    }
}